<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-col :span="24">
          <!-- 填报主题 -->
          <el-form-item label="填报主题" prop="topicId">
            <!--文本框-->
            <el-select v-model="dataForm.topicId" filterable placeholder="填报主题" clearable
              @change="topicChangeHandle(dataForm.topicId)" style="width:100%">
              <el-option v-for="item in topicList" :key="item.id" :label="item.title" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 填报单位 -->
          <el-form-item label="填报单位" prop="btypeId">
            <el-input v-model="dataForm.btypeName" placeholder="请选择" readonly>
              <el-button slot="append" icon="el-icon-search" @click.stop="show"></el-button>
            </el-input>
            <!--文本框-->
            <uni-btype-select v-if="dialogVisible" ref="btypeSelect" @backFun="backFun" :initDataForm="initDataForm"
              :multiple="false">
            </uni-btype-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 流程key -->
          <el-form-item label="流程" prop="procKey">
            <!--文本框-->
            <el-select v-model="dataForm.procKey" filterable placeholder="流程key" clearable style="width:100%">
              <el-option v-for="item in flowList" :key="item.id" :label="item.name" :value="item.key">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 排序 -->
          <el-form-item label="排序" prop="sort">
            <!--计数器-->
            <el-input-number v-model="dataForm.sort" :min="1" :max="50"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="status" label="状态" size="mini">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 备注说明 -->
          <el-form-item label="备注说明" prop="remark">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="备注说明"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      flowList: [],
      topicList: [],
      initDataForm: {},
      dialogVisible: false,
      dataForm: {
        id: '',
        topicId: '',
        procKey: '',
        sort: '',
        remark: '',
        status: 1,
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        btypeId: '',
        btypeName: '',
        startTime: '',
        endTime: '',
        unit: '',
        cycleType: '',
        typeDirection: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
        this.getFlowList()
        this.getTopicList()
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/udf/udfFillingTask/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 获取主题
    getTopicList () {
      this.$http.get(`/udf/udfFillingTopic/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.topicList = res.data
      }).catch(() => { })
    },
    // 获取流程
    getFlowList () {
      this.$http.get(`/act/process/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.flowList = res.data
      }).catch(() => { })
    },
    topicChangeHandle (topicId) {
      let topic = this.$getDataItem(this.topicList, topicId, 'id')
      this.dataForm.startTime = topic.startTime
      this.dataForm.endTime = topic.endTime
      this.dataForm.unit = topic.unit
      this.dataForm.typeDirection = topic.typeDirection
      this.dataForm.cycleType = topic.cycleType
    },
    show () {
      this.dialogVisible = true
      // 等待DOM完成更新 v-if 渲染后执行
      this.$nextTick(() => {
        this.$refs.btypeSelect.init()
      })
    },
    backFun (data) {
      this.dataForm.btypeId = data[0].id
      this.dataForm.btypeName = data[0].btypeName
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/udf/udfFillingTask/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
